# Maintainer: Martell Malone <martellmalone@gmail.com>
# Contributor: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>

#_variant=winrt #desktop
_variant=desktop

_realname=vlc
pkgbase=mingw-w64-${_realname}-git
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}-git"
provides=("${MINGW_PACKAGE_PREFIX}-${_realname}")
conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}")
_ver_base=2.2
pkgver=3.0.0.74242.d700f5d21e
pkgrel=1
pkgdesc="A multi-platform MPEG, VCD/DVD, and DivX player"
arch=('any')
url="https://www.videolan.org/vlc/"
license=('LGPL2.1' 'GPL2')
depends=("${MINGW_PACKAGE_PREFIX}-a52dec"
         "${MINGW_PACKAGE_PREFIX}-aribb24"
         "${MINGW_PACKAGE_PREFIX}-chromaprint"
         "${MINGW_PACKAGE_PREFIX}-faad2"
         "${MINGW_PACKAGE_PREFIX}-ffmpeg"
         "${MINGW_PACKAGE_PREFIX}-flac"
         "${MINGW_PACKAGE_PREFIX}-fluidsynth"
         "${MINGW_PACKAGE_PREFIX}-fribidi"
         "${MINGW_PACKAGE_PREFIX}-gnutls"
         "${MINGW_PACKAGE_PREFIX}-gsm"
         "${MINGW_PACKAGE_PREFIX}-libass"
         "${MINGW_PACKAGE_PREFIX}-libbluray"
         "${MINGW_PACKAGE_PREFIX}-libcaca"
         "${MINGW_PACKAGE_PREFIX}-libcddb"
         "${MINGW_PACKAGE_PREFIX}-libcdio"
         "${MINGW_PACKAGE_PREFIX}-libdca"
         "${MINGW_PACKAGE_PREFIX}-libdvdcss"
         "${MINGW_PACKAGE_PREFIX}-libdvdnav"
         "${MINGW_PACKAGE_PREFIX}-libdvbpsi"
         "${MINGW_PACKAGE_PREFIX}-libgme"
         "${MINGW_PACKAGE_PREFIX}-libgoom2"
         "${MINGW_PACKAGE_PREFIX}-libmad"
         "${MINGW_PACKAGE_PREFIX}-libmatroska"
         "${MINGW_PACKAGE_PREFIX}-libmpcdec"
         "${MINGW_PACKAGE_PREFIX}-libmpeg2"
         "${MINGW_PACKAGE_PREFIX}-libmysofa"
         "${MINGW_PACKAGE_PREFIX}-libplacebo"
         "${MINGW_PACKAGE_PREFIX}-libproxy"
         "${MINGW_PACKAGE_PREFIX}-librsvg"
         "${MINGW_PACKAGE_PREFIX}-libsamplerate"
         "${MINGW_PACKAGE_PREFIX}-libshout"
         "${MINGW_PACKAGE_PREFIX}-libssh2"
         "${MINGW_PACKAGE_PREFIX}-libtheora"
         "${MINGW_PACKAGE_PREFIX}-libvpx"
         "${MINGW_PACKAGE_PREFIX}-libxml2"
         "${MINGW_PACKAGE_PREFIX}-lua51"
         "${MINGW_PACKAGE_PREFIX}-opencv"
         "${MINGW_PACKAGE_PREFIX}-opus"
         "${MINGW_PACKAGE_PREFIX}-portaudio"
         "${MINGW_PACKAGE_PREFIX}-protobuf"
         "${MINGW_PACKAGE_PREFIX}-schroedinger"
         "${MINGW_PACKAGE_PREFIX}-speex"
         "${MINGW_PACKAGE_PREFIX}-taglib"
         "${MINGW_PACKAGE_PREFIX}-twolame"
         "${MINGW_PACKAGE_PREFIX}-vcdimager"
         "${MINGW_PACKAGE_PREFIX}-x264"
         "${MINGW_PACKAGE_PREFIX}-x265"
         "${MINGW_PACKAGE_PREFIX}-xpm-nox"
         "${MINGW_PACKAGE_PREFIX}-qt5"
         #"${MINGW_PACKAGE_PREFIX}-sdl_image"
         #"${MINGW_PACKAGE_PREFIX}-libtiger"
         #"${MINGW_PACKAGE_PREFIX}-zvbi"
         #"${MINGW_PACKAGE_PREFIX}-libupnp"
         #"${MINGW_PACKAGE_PREFIX}-libtar"
         )
makedepends=(
             "${MINGW_PACKAGE_PREFIX}-gcc"
             "pkg-config"
             "${MINGW_PACKAGE_PREFIX}-ncurses"
             "git"
             "autoconf"
             "automake"
             "libtool"
             "make"
             # 'libkate'
             # 'live-media'
             # 'libnotify'
             # 'libdc1394'
             # 'libavc1394'
             # 'xosd'
             # 'projectm'
             # 'aalib'
             # 'libmtp'
             )
# optdepends=("${MINGW_PACKAGE_PREFIX}-libnotify: for notification plugin"
            # "${MINGW_PACKAGE_PREFIX}-ncurses: for ncurses interface support"
            # "${MINGW_PACKAGE_PREFIX}-libdvdcss: for decoding encrypted DVDs"
            # "${MINGW_PACKAGE_PREFIX}-libavc1394: for devices using the 1394ta AV/C"
            # "${MINGW_PACKAGE_PREFIX}-libdc1394: for IEEE 1394 plugin"
            # "${MINGW_PACKAGE_PREFIX}-libbluray: for Blu-Ray support"
            # "${MINGW_PACKAGE_PREFIX}-flac: for Free Lossless Audio Codec plugin"
            # "${MINGW_PACKAGE_PREFIX}-portaudio: for portaudio support"
            # "${MINGW_PACKAGE_PREFIX}-twolame: for TwoLAME mpeg2 encoder plugin"
            # "${MINGW_PACKAGE_PREFIX}-projectm: for ProjectM visualisation plugin"
            # "${MINGW_PACKAGE_PREFIX}-libcaca: for colored ASCII art video output"
            # "${MINGW_PACKAGE_PREFIX}-libgme: for libgme plugin"
            # "${MINGW_PACKAGE_PREFIX}-librsvg: for SVG plugin"
            # "${MINGW_PACKAGE_PREFIX}-libgoom2: for libgoom plugin"
            # "${MINGW_PACKAGE_PREFIX}-vcdimager: navigate VCD with libvcdinfo"
            # "${MINGW_PACKAGE_PREFIX}-aalib: for ASCII art plugin"
            # "${MINGW_PACKAGE_PREFIX}-libmtp: for MTP devices support"
            # "${MINGW_PACKAGE_PREFIX}-libcdio: for audio CD playback support"
            # "${MINGW_PACKAGE_PREFIX}-opus: for opus support"
            # "${MINGW_PACKAGE_PREFIX}-libssh2: for sftp support")
#backup=('usr/share/vlc/lua/http/.hosts'
#        'usr/share/vlc/lua/http/dialogs/.hosts')
options=('strip' '!emptydirs')
source=(#"${_realname}-${_ver_base}"::"git+https://git.videolan.org/git/${_realname}/${_realname}-${_ver_base}.git"
        "${_realname}"::"git+https://git.videolan.org/git/vlc/${_realname}-3.0.git"
        0001-Use-libdir-for-plugins-on-msys2.patch
        0002-MinGW-w64-lfind-s-_NumOfElements-is-an-unsigned-int.patch
        0003-MinGW-w64-don-t-pass-static-to-pkg-config-if-SYS-min.patch
        0004-Revert-Win32-prefer-the-static-libraries-when-creati.patch
        0005-Remove-legacy-defines.patch
        0006-Linking-libqt_plugin-with-winmm.patch
        0007-Mingw-load-libraries-not-only-from-system32.patch
        0011-Add-include-on-WINSTORE-for-QueueTimer-functions.patch
        0012-Only-build-correct-modules-for-WINSTORE.patch
        0013-remove-AllocConsole-reference-for-WINSTORE.patch
        0014-Remove-some-legacy-mingw.org-header-defines.patch
        0015-x264-more-than-153-support.patch)
sha256sums=('SKIP'
            '643273cf1fd156e8990c1de441214bc75aeb5317d8aea8570e66fd8d836c23f0'
            '8f40d57bb087306e5099c5b9067dacca0479c4dc9e45eb902ad04eeb3fb83ef3'
            '072de5cda7772a63d81a067be1b942e1cf59f6b53206e61d0caac9dc18216ad6'
            '416f6c11eb8a47ea3d0f258fa7d471e0eb53b79e4816913d083aacead0113e7a'
            '612b7718c76b23ed0f69ed28b06d3b67ae89b1764b62dc7443e9edc26a541e2e'
            '71eada1e1a93e77f7a6c76423e4d39e9f8f700ca12322c3ffa7ecfaa3debf5ae'
            '194b85e50f2c410ce50d7c7ff23bc915020c9c073a83f64f12c8247d41e1b475'
            '3c55d8e00868b95addaaf174b2348a2ce915fc59208c61bd438c8834290712d3'
            'a83eecee3b2431addd071b964f4febe6dc3b1b310b36ed6934e8fa2d8f5c4c47'
            '64a1a0efd69e8ebb36df08c45ff8ae71e1b90d2c78ee7fd3229739c2d76a384d'
            'cb07edb1c4db96a9ee85aae0d32ae6b491dc9c433058507031050455e0bb3d06'
            'c96a46b638d6ed665e7665403c8de52572230ddf3cbf1e63cab71fcd26b31520')

pkgver() {
  cd ${_realname}
  local _ver=$(grep AC_INIT configure.ac | sed -e 's/AC_INIT([^,]\+,\s*\[\?\([0-9.a-z-]\+\).*/\1/')
  #or use local _ver=$_ver_base
  _ver=${_ver%-*}
  revision=$(git rev-list --count HEAD)
  hash=$(git log --pretty=format:'%h' -n 1)
  echo ${_ver}.${revision}.${hash}
}

prepare() {
  cd "${srcdir}"/${_realname}

  if [ "${_variant}" != "winrt" ]; then
    git am --committer-date-is-author-date "${srcdir}"/0001-Use-libdir-for-plugins-on-msys2.patch
  fi

    git am --committer-date-is-author-date "${srcdir}"/0002-MinGW-w64-lfind-s-_NumOfElements-is-an-unsigned-int.patch
    git am --committer-date-is-author-date "${srcdir}"/0003-MinGW-w64-don-t-pass-static-to-pkg-config-if-SYS-min.patch
    git am --committer-date-is-author-date "${srcdir}"/0004-Revert-Win32-prefer-the-static-libraries-when-creati.patch
    git am --committer-date-is-author-date "${srcdir}"/0005-Remove-legacy-defines.patch
    git am --committer-date-is-author-date "${srcdir}"/0006-Linking-libqt_plugin-with-winmm.patch
    git am --committer-date-is-author-date "${srcdir}"/0007-Mingw-load-libraries-not-only-from-system32.patch
    git am --committer-date-is-author-date "${srcdir}"/0015-x264-more-than-153-support.patch

  if [ "${_variant}" == "winrt" ]; then
    git am --committer-date-is-author-date "${srcdir}"/0011-Add-include-on-WINSTORE-for-QueueTimer-functions.patch
    git am --committer-date-is-author-date "${srcdir}"/0012-Only-build-correct-modules-for-WINSTORE.patch
    git am --committer-date-is-author-date "${srcdir}"/0013-remove-AllocConsole-reference-for-WINSTORE.patch
    git am --committer-date-is-author-date "${srcdir}"/0014-Remove-some-legacy-mingw.org-header-defines.patch
  fi

  ./bootstrap

  #sed -i -e 's:truetype/freefont:TTF:g' modules/text_renderer/freetype.c
  #sed -i -e 's:truetype/ttf-dejavu:TTF:g' modules/visualization/projectm.cpp
}

build() {

  mkdir -p ${srcdir}/build-${MINGW_CHOST}
  cd ${srcdir}/build-${MINGW_CHOST}

  # Hack getting private QtGui directory
  local _qtver=$(qmake -query QT_VERSION)
  local _qtguiinc=$(pkg-config Qt5Gui --variable=includedir)
  local _privdir=${_qtguiinc}/QtGui/${_qtver}/QtGui
  CPPFLAGS+=" -I${_privdir}"

if [ "${_variant}" = "winrt" ]; then

  #WinRT
  EXTRA_CPPFLAGS+="-D_WIN32_WINNT=0x602 -DWINVER=0x602 -DWINSTORECOMPAT -D_UNICODE -DUNICODE -DWINAPI_FAMILY=WINAPI_FAMILY_APP -DHAVE_WINRT"
  EXTRA_CFLAGS+="-DNDEBUG ${EXTRA_CPPFLAGS}"
  EXTRA_LDFLAGS+=" -lnormaliz -lwinstorecompat -lruntimeobject"

  ${MINGW_CHOST}-gcc -dumpspecs | sed -e 's/-lmingwex/-lwinstorecompat -lmingwex -lwinstorecompat -lole32 -lruntimeobject/' -e 's/-lmsvcrt/-lmsvcr110/' > newspecfile

  NEWSPECFILE="`pwd`/newspecfile"

  echo "$NEWSPECFILE"

  #WinRT
  CFLAGS="${EXTRA_CFLAGS}" \
  CPPFLAGS="${EXTRA_CPPFLAGS}" \
  LDFLAGS="${EXTRA_LDFLAGS}" \
  CC="${MINGW_CHOST}-gcc -specs=$NEWSPECFILE" \
  CXX="${MINGW_CHOST}-g++ -specs=$NEWSPECFILE" \
  BUILDCC="${MINGW_PREFIX}/bin/gcc" \
  ac_cv_search_connect="-lws2_32" \
  ${srcdir}/${_realname}/configure \
    --prefix=${MINGW_PREFIX} \
    --sysconfdir=${MINGW_PREFIX}/etc \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --enable-winstore-app \
    --disable-vlc \
    --enable-lua \
    --disable-vlm \
    --disable-sout \
    --disable-faad \
    --disable-flac \
    --enable-theora \
    --disable-twolame \
    --enable-quicktime \
    --enable-avcodec \
    --enable-merge-ffmpeg \
    --enable-dca \
    --enable-mpc \
    --enable-libass \
    --disable-x264 \
    --enable-realrtsp \
    --enable-shout \
    --disable-goom \
    --disable-caca \
    --disable-qt \
    --disable-skins2 \
    --disable-libcddb \
    --disable-zvbi \
    --disable-telx \
    --enable-mad \
    --disable-a52 \
    --disable-libgcrypt \
    --disable-taglib \
    --disable-ncurses \
    --disable-dbus \
    --disable-lua

    # Having some issues with lua 5.2, disable for now
    #LUAC==${MINGW_PREFIX}/bin/luac51  LUA_LIBS="`pkg-config --libs lua`" \
    #RCC==${MINGW_PREFIX}/bin/rcc

#todo: look at direct for winRT
# --disable-dirac
#todo: check if sse and mmx are used already
# --enable-sse \
# --enable-mmx \

else
  BUILDCC="${MINGW_PREFIX}/bin/gcc" \
  ${srcdir}/${_realname}/configure \
    --prefix=${MINGW_PREFIX} \
    --sysconfdir=${MINGW_PREFIX}/etc \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --enable-qt \
    --disable-ncurses \
    --disable-dbus \
    --disable-telx \
    --enable-nls \
    --disable-lua \
    --disable-gst-decode

    # Having some issues with lua 5.2, disable for now
    #LUAC==${MINGW_PREFIX}/bin/luac51  LUA_LIBS="`pkg-config --libs lua`" \
    #RCC==${MINGW_PREFIX}/bin/rcc

fi

  make
}

package() {
  cd ${srcdir}/build-${MINGW_CHOST}

  #todo: fix install for winrt build
  make DESTDIR="${pkgdir}" install

  for res in 16 32 48 128; do
    install -D -m644 "${srcdir}/${_realname}/share/icons/${res}x${res}/vlc.png" \
                     "${pkgdir}/${MINGW_PREFIX}/share/icons/hicolor/${res}x${res}/apps/vlc.png"
  done

  #todo: patch vlc for plugins to be in share directory
  #mv ${pkgdir}/${MINGW_PREFIX}/lib/vlc/plugins ${pkgdir}/${MINGW_PREFIX}/bin
}
